//
//  QCDrivingController.swift
//  爱汽车
//
//  Created by liujianlong on 2023/8/25.
//

import UIKit

class QCDrivingController: QCViewController {
    var array = ["FRONT","REAR"]
    var values = [0,0]
    var limits = [(max:100,min:0),(max:100,min:0)]
    override func viewDidLoad() {
        super.viewDidLoad()
        initUI()
    }
    
    override func initUI() {
        title = LS("最低行驶保护气压")
        tableView.delegate = self
        tableView.dataSource = self
        tableView.register(QCDrivingCell.nib(), forCellReuseIdentifier: QCDrivingCell.ID())
        tableView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
    }
    
    override var bytes: [UInt8]{
        didSet{
            let ack = bytes[7]
            if ack != UInt8(AIQC.userInfo.ack){return}
            if recived == false{
                recived = true
                return
            }
            let data = [UInt8](bytes[AI_index..<bytes.count])
            values = [Int(data[92]), Int(data[94])]
            tableView.reloadData()
        }
    }
    
    
    func action(_ value:Int, _ row:Int){
        recived = false
        values[row] = value
        let front = "\(values[0])".toHex2()
        let rear = "\(values[1])".toHex2()
        BLEData.appSetDataToPer(AIQC.protectionPreMin.cmd, front+front+rear+rear).write
    }
}
//MARK: - UITableViewDataSource
extension QCDrivingController:UITableViewDataSource
{
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return array.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: QCDrivingCell.ID()) as! QCDrivingCell
        cell.title.text = LS(array[indexPath.row])
        cell.limit = limits[indexPath.row]
        cell.value = values[indexPath.row]
        cell.block = {[weak self] value in
            self?.action(value, indexPath.row)
        }
        return cell
    }
}
//MARK: - UITableViewDelegate
extension QCDrivingController:UITableViewDelegate{
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    }
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 0.01
    }
    
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 0.01
    }
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        return nil
    }
    
    func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        return nil
    }
}
